﻿using StockHelper.Scripts.Comm;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StockHelper.Scripts.Sound
{
    public class VoicePlay
    {
        private static List<string> SoundList;
        


        public static void Play()
        {
            if(Config.playVoice)
            Sound.Play(PlaySoundList());
        }



        public static void Play2()
        {
                Sound.Play(PlaySoundList());
        }



        public static void ComposeVoice()
        {
            SoundList = new List<string>();
            foreach (Hashtable data in Config.Get("voicePlay", new ArrayList()))
            {
                if (data.ContainsKey("isPrice") && data["isPrice"].ToString() == "1")
                {

                }
                else
                {
                    if (data.ContainsKey("isDownload") && data["isDownload"].ToString() == "1")
                    {
                        if (data.ContainsKey("path") && File.Exists(data["path"].ToString()))
                        {
                            SoundList.Add(data["path"].ToString());
                        }
                        else
                        {

                        }
                    }
                }
            }





            Sound.Play(SoundList);
        }

        public static void DownloadVoices() {



        }





        public static List<string> PlaySoundList()
        {
            List<string> list = new List<string>();

            foreach (Hashtable data in Config.Get("voicePlay", new ArrayList()))
            {
                if (data.ContainsKey("isPrice") && data["isPrice"].ToString() == "1")
                {
                    if (Stock.StockCtrl.GetData(data["url"].ToString()) == null)
                        continue;
                    Assets.Package _data = Assets.GetData(data["jsonPath"].ToString());
                    Hashtable priceData = _data.price[data["key"]] as Hashtable;
                    bool passTail = priceData.ContainsKey("passTail") && (priceData["passTail"].ToString() == "1" || (bool)priceData["passTail"] == true);
                    bool 涨跌 = (bool)Stock.StockCtrl.GetData(data["url"].ToString(), Types.Param.是否涨价);
                    string price = Stock.StockCtrl.GetData(data["url"].ToString(), Types.Param.当前价格).ToString();

                    if(priceData.ContainsKey("type"))
                    switch (priceData["type"].ToString()) {
                            case "difference": //涨跌差价
                                price = Stock.StockCtrl.GetData(data["url"].ToString(), Types.Param.涨跌值).ToString();
                                break;
                            case "percentage": //涨跌百分比
                                price = ((float)Stock.StockCtrl.GetData(data["url"].ToString(), Types.Param.涨跌百分比f) *100 ).ToString();
                                break;
                        }


                    foreach (string key in priceData["compose"] as ArrayList)
                    {
                        if (Regex.IsMatch(key, @"\[.*\]"))
                        {
                            string kk = GetPriceKey(price, key, priceData, 涨跌);
                            if (!string.IsNullOrEmpty(kk))
                            {
                                if (_data.keys.ContainsKey(kk))
                                {
                                    list.Add(_data.path + ((Hashtable)_data.keys[kk])["sound"].ToString());
                                }
                                else
                                {
                                    MessageBox.Show("缺少配置项:" + data["jsonPath"].ToString() + "  " + kk);
                                }
                            }
                        }
                        else
                        {
                            list.Add(_data.path + ((Hashtable)_data.keys[key])["sound"].ToString());
                        }
                    }

                }
                else
                {
                    list.Add(
                        Config.voicesPath + data["sound"].ToString()
                        );
                }
            }
            return list;
        }

        public static string GetPriceKey(string price, string key, Hashtable data, bool 涨跌)
        {
            char[] p = price.ToArray();
            int i = price.IndexOf('.');
            bool de = true;
            if (i < 0)
            {
                i = price.Length ;
                de = false;
            }
            switch (key)
            {
                case "[0000]":
                    if (i - 4 >= 0 && p[i - 4].ToString() != "-")
                        return (data["[0000]"] as ArrayList)[int.Parse(p[i - 4].ToString())].ToString();
                    break;
                case "[000]":
                    if (i - 3 >= 0 && p[i - 3].ToString() != "-")
                        return (data["[000]"] as ArrayList)[int.Parse(p[i - 3].ToString())].ToString();
                    break;
                case "[00]":
                    if (i - 2 >= 0 && p[i - 2].ToString() != "-")
                        return (data["[00]"] as ArrayList)[int.Parse(p[i - 2].ToString())].ToString();
                    break;
                case "[0]":
                    if (i - 1 >= 0 && p[i - 1].ToString() != "-")
                        return (data["[0]"] as ArrayList)[int.Parse(p[i - 1].ToString())].ToString();
                    break;
                case "[.0]":
                    if (de && i + 1 >= 0 && p[i + 1].ToString() != "-")
                        return (data["[.0]"] as ArrayList)[int.Parse(p[i + 1].ToString())].ToString();
                    break;
                case "[.00]":
                    if (de && i + 2 >= 0 && p[i + 2].ToString() != "-")
                    {
                        return (data["[.00]"] as ArrayList)[int.Parse(p[i + 2].ToString())].ToString();
                    }
                    break;
                case "[compare]":
                    int c = 涨跌 ? 1 : 0;
                    return (data[key] as ArrayList)[c].ToString();
            }
            return null;
        }




        public class Data
        {
            public string name;
            public string path;
            public int index;
            public int isPrice;
            public string jsonPath;
        }

    }
}
